package com.zy.cyan.action;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionContext;
import com.zy.cyan.utils.CommonUtil;

@Component("ajaxAction")
@Scope("prototype")
public class AjaxAction extends BaseAction {
	private JSONArray result;
	private String albumName;
	private JSONArray albumResource; // for album ajax

	public String getAlbum() throws InterruptedException {
		ActionContext ctx = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) ctx
				.get(ServletActionContext.HTTP_REQUEST);
		albumResource = CommonUtil.getPicturesPath(request.getSession()
				.getServletContext().getRealPath("/albums/" + albumName));
		return "getAlbum";
	}

	public String getArticles() throws InterruptedException {
		result = new JSONArray();
		Thread.sleep(1000);
		for (int i = 0; i < 4; ++i) {
			JSONObject obj = new JSONObject();
			obj.put("title", "title " + i);
			obj.put("cont", "content " + i);
			result.add(obj);
		}
		return "getArticles";
	}

	public JSONArray getResult() {
		return result;
	}

	public void setResult(JSONArray result) {
		this.result = result;
	}

	public String getAlbumName() {
		return albumName;
	}

	public void setAlbumName(String albumName) {
		this.albumName = albumName;
	}

	public JSONArray getAlbumResource() {
		return albumResource;
	}

	public void setAlbumResource(JSONArray albumResource) {
		this.albumResource = albumResource;
	}
}